import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { Platform, ToastController } from '@ionic/angular';
import { Pipe, PipeTransform } from '@angular/core';

@Component({
  selector: 'app-listfilter',
  templateUrl: './listfilter.page.html',
  styleUrls: ['./listfilter.page.scss'],
})

export class ListfilterPage implements OnInit {

  page: any = 1;//页码
  limit: any = 15;//条数
  showtitle = false;

  public hasMore = true;
  typename;
  sdate;
  edate;
  gjz;
  filterdate: Filter = { typename: '', sdate: '', edate: '', gjz: '' };


  public items: Array<{
    mprGUID: string, subject: string; content: string
    , pushtime: string, ensure: string, browsestate: string, PushType: string,
    note: string, pushusername: string
  }> = [];
  public typeitems: Array<{ guid: string, type: string }> = [];
  //  userId:any=   AppComponent.prototype.userinfo.GUID;//用户id
  userId: any = "";//用户id




  constructor(
    public router: Router,
    private platform: Platform,
    public activeRoute: ActivatedRoute,
    public toastCtrl: ToastController, ) {
    this.items = [];
  }

  ngOnInit() {
    this.gettypedata();
    this.getdata();
    this.activeRoute.queryParams.subscribe((params: Params) => {
      this.showtitle = params['showtitle'];
    })
  }
  search() {
    alert(this.typename + "--" + this.sdate + "-=" + this.edate + "==" + this.gjz);
    if (this.typename != undefined && this.typename.length != 0) {
      this.filterdate.typename = this.typename;
    } else {
      this.filterdate.typename = '';
    }
    if (this.sdate != undefined && this.sdate.length != 0) {
      this.filterdate.sdate = this.sdate;
    } else {
      this.filterdate.sdate = '';
    } if (this.edate != undefined && this.edate.length != 0) {
      this.filterdate.edate = this.edate;
    } else {
      this.filterdate.edate = '';
    } if (this.gjz != undefined && this.gjz.length != 0) {
      this.filterdate.gjz = this.gjz;
    } else {
      this.filterdate.gjz = '';
    }
  }
  loadData(e) {
    setTimeout(() => {
      console.log('Done');
      ++this.page;
      e ? e.target.complete() : '';
      if (this.items.length > 30) {
        e ? e.target.disabled = true : '';
        this.hasMore = false;
      }
    }, 500);
  }
  gettypedata() {

    this.typeitems = [{ guid: "1", type: "类型1" }, { guid: "2", type: "类型2" }, { guid: "3", type: "类型3" },];
    this.typeitems.splice(0, 0, { guid: "0", type: "全部" });
  }
  getdata() {
    // this.http.getMessageList(this.userId).subscribe(data=>{
    //   this.items = data["data"];
    // })
    this.items = [{
      mprGUID: 'guid', subject: '主题', content: '内容'
      , pushtime: '2019-12-13', ensure: '0', browsestate: '已接收', PushType: '类型',
      note: '备注', pushusername: '发送人',
    }, {
      mprGUID: 'guid', subject: '主题1', content: '内容1'
      , pushtime: '2019-12-13', ensure: '0', browsestate: '已接收1', PushType: '类型1',
      note: '备注1', pushusername: '发送人1',
    }, {
      mprGUID: 'guid', subject: '主题1', content: '内容'
      , pushtime: '2019-12-13', ensure: '0', browsestate: '已接收', PushType: '类型2',
      note: '备注', pushusername: '发送人',
    }];

  }


}




@Pipe({
  name: 'myfilter',
  pure: false
})
export class MyFilterPipe implements PipeTransform {

  transform(items: any[], filter: Filter): any {
    if (filter.typename === '' && filter.sdate === '' && filter.edate === '' && filter.gjz === '') {
      return items;
    }

    // filter items array, items which match and return true will be
    // kept, false will be filtered out
    return items.filter(item => {
      console.log(filter);

      var f1 = true;
      var f2 = true;
      var f3 = true;
      var sdate = filter.sdate;
      var edate = filter.edate;


      f1 = '' === filter.typename || '全部' === filter.typename || item.PushType === filter.typename;

      if (sdate.length != 0 && edate.length != 0) {
        var ds = new Date(sdate);
        var de = new Date(edate);
        var da = new Date(item.pushtime);
        f2 = da.getTime() >= ds.getTime() && da.getTime() <= de.getTime()
      }
      if (filter.gjz.length != 0) {
        f3 = item.content.indexOf(filter.gjz) >= 0 || item.subject.indexOf(filter.gjz) >= 0
      }

      return f1 && f2 && f3


    });
  }
}
export class Filter { gjz: string; sdate: string; edate: string; typename: string }
